//
//  Company_DetailHeaderView2.m
//  HuaRun2
//
//  Created by admin on 2017/9/9.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "Company_DetailHeaderView2.h"
#import "LineView.h"

@interface Company_DetailHeaderView2 ()

@property (nonatomic, strong) UIView * backView;

//线1
@property (nonatomic, strong) LineView * line1;
//线2
@property (nonatomic, strong) LineView * line2;
@end

@implementation Company_DetailHeaderView2

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

#pragma mark - life cycle

- (void)dealloc {
    NSLog(@"%@ dealloc", [self class]);
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self viewInit];
        [self layoutInit];
    }
    return self;
}

- (void)viewInit {
    self.backgroundColor = RGBA(249, 249, 249, 1);
    
    [self addSubview:self.backView];
    [self addSubview:self.nameLab];
    [self addSubview:self.companyLab];
    
    [self addSubview:self.noteView];
    
    [self addSubview:self.roleView];
    [self addSubview:self.meetingView];
    [self addSubview:self.giftView];
    
    [self addSubview:self.line1];
    [self addSubview:self.line2];

    [self addSubview:self.crcFlagView];
    [self addSubview:self.groupLab];
    
}

- (void)layoutInit {
    weakself(ws);
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws).offset(kMargin10);
        make.right.mas_equalTo(ws).offset(-kMargin10);
        make.top.mas_equalTo(ws).offset(0);
        make.bottom.mas_equalTo(ws);
    }];
    
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.backView).offset(kMargin10);
        make.top.mas_equalTo(ws.backView).offset(kMargin15);
        make.right.mas_equalTo(ws.noteView.mas_left).offset(-kMargin10 );
    }];


    [self.companyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.nameLab);
        make.top.mas_equalTo(ws.nameLab.mas_bottom).offset(kMargin10 / 2);
        make.right.mas_equalTo(ws.backView).offset(-kMargin10 );
    }];
    
    [self.groupLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.nameLab);
        make.top.mas_equalTo(ws.companyLab.mas_bottom).offset(kMargin15);
        make.width.mas_equalTo(70);
    }];

    [self.crcFlagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.groupLab.mas_right).offset(kMargin10/2);
        make.centerY.mas_equalTo(ws.groupLab);
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
    
    [self.noteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(ws.backView);
        make.top.mas_equalTo(ws.nameLab);
        make.size.mas_equalTo(CGSizeMake(90, 20));
    }];
    
    CGFloat width = (kMainScreenWidth - kMargin10 * 4 -kMargin10 * 2) / 2;
    [self.meetingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.backView.mas_centerX).offset(kMargin10);
        make.bottom.mas_equalTo(ws.backView.mas_bottom).offset(-kMargin15);
        make.size.mas_equalTo(CGSizeMake(width, 25));
    }];
    
    [self.roleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(ws.backView.mas_centerX).offset(-kMargin10);
        make.size.bottom.mas_equalTo(ws.meetingView);
    }];

    
    
//    [self.meetingView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.mas_equalTo(ws.backView);
//        make.bottom.mas_equalTo(ws.backView.mas_bottom).offset(-kMargin15);
//        make.size.mas_equalTo(CGSizeMake(width, 25));
//    }];
//    
//    [self.roleView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(ws.meetingView.mas_left).offset(-kMargin10 * 2);;
//        make.size.bottom.mas_equalTo(ws.meetingView);
//    }];
    
//    [self.giftView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(ws.meetingView.mas_right).offset(kMargin10 * 2);;
//        make.size.bottom.mas_equalTo(ws.meetingView);
//    }];
    
    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.mas_equalTo(ws.roleView);
        make.width.mas_equalTo(1);
        make.left.mas_equalTo(ws.roleView.mas_right).offset(kMargin10);
    }];
    
//    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.centerY.mas_equalTo(ws.roleView);
//        make.width.mas_equalTo(1);
//        make.left.mas_equalTo(ws.meetingView.mas_right).offset(kMargin10);
//    }];
    
}
#pragma mark - getter

- (UIView *)backView {
    if (!_backView) {
        self.backView = [UIView new];
        _backView.backgroundColor = [UIColor whiteColor];
    }
    return _backView;
}

- (UILabel *)nameLab {
    if (!_nameLab) {
        self.nameLab = [UILabel new];
        _nameLab.textAlignment = NSTextAlignmentLeft;
        _nameLab.font = [UIFont boldSystemFontOfSize:15];// kFontSize15;
        _nameLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
//        _nameLab.text = @"name";
    }
    return _nameLab;
}

- (UILabel *)companyLab {
    if (!_companyLab) {
        self.companyLab = [UILabel new];
        _companyLab.textAlignment = NSTextAlignmentLeft;
        _companyLab.font =  [UIFont boldSystemFontOfSize:15];//kFontSize15;
        _companyLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
//        _companyLab.text = @"company name";
    }
    return _companyLab;
}
- (UIView *)noteView {
    if (!_noteView) {
        self.noteView = [self makeCommonLabelWithText:NSLocalizedString(@"查看备注",nil) imageName:@"最近查看"];
    }
    return _noteView;
}

- (UIButton *)roleView {
    if (!_roleView) {
        self.roleView = [self makeCommonButtonWithText:NSLocalizedString(@"联系人",nil) normalImage:@"联系人001"];
    }
    return _roleView;
}
- (UIButton *)meetingView {
    if (!_meetingView) {
        self.meetingView = [self makeCommonButtonWithText:NSLocalizedString(@"会议",nil) normalImage:@"会议001"];
    }
    return _meetingView;
}

- (UIButton *)giftView {
    if (!_giftView) {
        self.giftView = [self makeCommonButtonWithText:NSLocalizedString(@"礼物",nil) normalImage:@"liwu"];
        self.giftView.hidden = YES;
    }
    return _giftView;
}


- (LineView *)line1 {
    if (!_line1) {
        self.line1 = [LineView new];
    }
    return _line1;
}

- (LineView *)line2 {
    if (!_line2) {
        self.line2 = [LineView new];
        self.line2.hidden = YES;
    }
    return _line2;
}

- (UILabel *)groupLab {
    if (!_groupLab) {
        self.groupLab = [UILabel new];
        _groupLab.textAlignment = NSTextAlignmentLeft;
        _groupLab.font = kGeneralFontSize;
        _groupLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
        _groupLab.text = @"CRC Group";
        _groupLab.backgroundColor = kRandomColor;
    }
    return _groupLab;
}

- (UIButton *)crcFlagView {
    if (!_crcFlagView) {
        self.crcFlagView = [UIButton new];
        [_crcFlagView setBackgroundImage:[UIImage imageNamed:@"未选中"]
                                forState:(UIControlStateNormal)];
        [_crcFlagView setBackgroundImage:[UIImage imageNamed:@"私有备注"]
                                forState:(UIControlStateSelected)];
    }
    return _crcFlagView;
}
- (UIView *)makeCommonLabelWithText:(NSString *)text
                          imageName:(NSString *)imageName {
    UIView * view = [[UIView alloc] init];
    view.backgroundColor = kRandomColor;
    view.frame = CGRectMake(0, 0, 120, 20);
    
    UIImageView * iconView = [UIImageView new];
    iconView.image = [UIImage imageNamed:imageName];
    iconView.frame = CGRectMake(0, 5, 15, 10);
    [view addSubview:iconView];
    
    UILabel * companyLab = [UILabel new];
    companyLab.textAlignment = NSTextAlignmentLeft;
    companyLab.frame = CGRectMake(20, 0, 100, 20);
    companyLab.font = kGeneralFontSize;
    companyLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
    companyLab.text = text;
    [view addSubview:companyLab];
    
    return view;
}

- (UIView *)makeCommonLabel2WithText:(NSString *)text
                           imageName:(NSString *)imageName{
    UIView * view = [[UIView alloc] init];
    view.backgroundColor = kRandomColor;
//    view.frame = CGRectMake(0, 0, 100, 30);
    [UIColor whiteColor];
    UIImageView * iconView = [UIImageView new];
    iconView.image = [UIImage imageNamed:imageName];
//    iconView.frame = CGRectMake(0, 7.5, 15, 15);
    [view addSubview:iconView];
    
    UILabel * companyLab = [UILabel new];
    companyLab.textAlignment = NSTextAlignmentLeft;
//    companyLab.frame = CGRectMake(20, 5, 70, 20);
    companyLab.font = kGeneralFontSize;
    companyLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
    companyLab.text = text;
    companyLab.tag = 200;
    [view addSubview:companyLab];
    
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(view);
        make.centerY.mas_equalTo(view);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    
    [companyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(iconView.mas_right).offset(kMargin10 / 2);
        make.centerY.mas_equalTo(view);
        make.right.mas_equalTo(view);
        make.height.mas_equalTo(20);
    }];
    
    return view;
}

- (void)commonReSetNum:(NSString *)num view:(UIButton *)view {
    NSString * title ;
    if (view == _roleView) {
        title = NSLocalizedString(@"联系人",nil);
    }else if (view == _meetingView) {
        title = NSLocalizedString(@"会议", nil);
    }else if (view == _giftView) {
        title = NSLocalizedString(@"礼物", nil);
    }
    
    [view setTitle:[NSString stringWithFormat:@"%@(%@)", title,
                    num]
          forState:(UIControlStateNormal)];
}
- (void)resetContactNum:(NSString *)num {
    
    [self commonReSetNum:num view:self.roleView];
}
- (void)resetMeetingNum:(NSString *)num {
    [self commonReSetNum:num view:self.meetingView];
}

- (void)resetGiftNum:(NSString *)num {
    [self commonReSetNum:num view:self.giftView];
}

- (UIButton *)makeCommonButtonWithText:(NSString *)text
                           normalImage:(NSString *)normalImage{
    
    UIButton * btn = [UIButton new];
    [btn setImage:[UIImage imageNamed:normalImage] forState:(UIControlStateNormal)];
    [btn setTitleColor:[UIColor colorWithHex:@"3c3c3c"] forState:(UIControlStateNormal)];
    [btn setTitleColor:RGBA(243, 161, 50, 1) forState:(UIControlStateSelected)];
    //    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:text forState:UIControlStateNormal];
    [btn setTitle:text forState:UIControlStateSelected];
    [btn.titleLabel setFont:kGeneralFontSize];
    [btn centerButtonAndImageWithSpacing:kMargin10];
    return btn;
}

@end
